package com.jdy.haoduoai.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jdy.haoduoai.LeXiaoXiaoBanApp;
import com.jdy.haoduoai.adapter.NormalStoryItemAdapter;
import com.jdy.haoduoai.model.LeUser;
import com.jdy.haoduoai.model.NormalStoryitemVo;
import com.jdy.haoduoai.util.CommonUtil;
import com.jdy.haoduoai.view.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.ycsj.goldmedalnewconcept.R;

import org.droidparts.annotation.inject.InjectView;

import java.util.ArrayList;

//import com.jdy.haoduoai.R;

/**
 * 普通故事集详情
 * 
 * @author Administrator
 * 
 */
public class NormalStorysdetails extends BaseActivity implements
    OnClickListener, OnItemClickListener {

  @InjectView(id = R.id.backs, click = true)
  private View back;
  @InjectView(id = R.id.cover, click = true)
  private RoundImageView roundImage;
  @InjectView(id = R.id.detailbtn, click = false)
  private ImageButton detailbtn;
  @InjectView(id = R.id.name)
  private TextView name;
  @InjectView(id = R.id.listview)
  private ListView listView;
  @InjectView(id = R.id.viptips)
  private LinearLayout viplyt;
  @InjectView(id = R.id.openVip, click = true)
  private Button openVip;
  @InjectView(id = R.id.detaillyt)
  private LinearLayout detaillyt;
  @InjectView(id = R.id.close, click = true)
  private ImageButton close;
  @InjectView(id = R.id.playtimes)
  private TextView playtimes;
  @InjectView(id = R.id.updatetime)
  private TextView updatetime;
  @InjectView(id = R.id.forage)
  private TextView forage;
  @InjectView(id = R.id.smmmary)
  private TextView smmary;

  private ArrayList<NormalStoryitemVo> mList;
  private NormalStoryItemAdapter adapter;
  private DisplayImageOptions options;
  private LeUser mLeUser;

  @Override
  @SuppressLint("InflateParams")
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.normalstorydetails);
    hideTitleView();
    initialize();
    initListener();
    requestData();
  }

  private void initialize() {
    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.default_img_listitem) // 下载 失败 显示的 图片
        .cacheInMemory(true) // 缓存
        .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
        .build();
    mList = new ArrayList<NormalStoryitemVo>();
    mLeUser = LeXiaoXiaoBanApp.getInstance().getUser();
    SpannableString string = new SpannableString(
        "故事简介:教师节后视镜教师节仅仅是教师节教师节是教师节教师节是教师节教师节是教师节是教师节教师节是教师节是蒋介石接受介绍");
    string.setSpan(new TextAppearanceSpan(this, R.style.stylea), 0, 4,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    string.setSpan(new TextAppearanceSpan(this, R.style.styleb), 4,
        string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    smmary.setText(string, TextView.BufferType.SPANNABLE);
  }

  private void initListener() {
    listView.setOnItemClickListener(this);
    openVip.setOnTouchListener(CommonUtil.VIEW_TOUCH_DARK);
  }

  private void requestData() {
    NormalStoryitemVo vo = new NormalStoryitemVo();
    vo.no = "01"; // );
    vo.title = "猪猪侠8变身战队 第1集(15:26)"; // );
    vo.descrtion = "英雄!一分为五"; // );
    vo.isNeedVip = true; // );
    mList.add(vo);
    mList.add(vo);
    mList.add(vo);
    mList.add(vo);
    mList.add(vo);
    mList.add(vo);

    if (adapter == null) {
      adapter = new NormalStoryItemAdapter(this, mList);
      listView.setAdapter(adapter);
    } else {
      adapter.notifyDataSetChanged();
    }
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.backs:
      finish();
      break;
    case R.id.cover:
      if (!detaillyt.isShown()) {
        detaillyt.setVisibility(View.VISIBLE);
      }
      break;
    case R.id.openVip:
      Intent intent = new Intent(NormalStorysdetails.this,
          MembershipCenterActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
          | Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(intent);
      break;
    case R.id.close:
      if (detaillyt.isShown()) {
        detaillyt.setVisibility(View.GONE);
      }
      break;
    default:
      break;
    }

  }

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    NormalStoryitemVo storyitemVo = mList.get(arg2);
    if (storyitemVo.isNeedVip && !viplyt.isShown()) {
      viplyt.setVisibility(View.VISIBLE);
    } else {
      viplyt.setVisibility(View.GONE);
      toastShow(storyitemVo.no + "播放");
    }

  }
}
